﻿﻿using System;
using UnityEngine;

namespace Com.Enemy.EnemySystem.Tools
{
    public class CoolDownOrder<T> where T : ICoolDown<T>
    {
        private readonly T[] items;
        private int currentItemCount;

        public CoolDownOrder(int maxOderSize)
        {
            items = new T[maxOderSize];
        }

        public void Add(T item)
        {
            for (int i = 0; i < currentItemCount; i++)
            {
                if(item.CompareTo(items[i]) > 0)
                {
                    var temp = items[i];
                    items[i] = item;
                    item = temp;
                }
            }
            items[currentItemCount] = item;
            currentItemCount++;
        }

        public float Count => currentItemCount;
        
        public void UpdateCoolDown(float time)
        {
            for (int i = 0; i < currentItemCount; i++)
            {
                items[i].NowCoolDown -= time;
                if (items[i].NowCoolDown <= 0)
                {
                    currentItemCount--;
                }
            }
        }

        public bool Contains(int itemTag)
        {
            for (int i = 0; i < currentItemCount; i++)
            {
                if (itemTag == items[i].ItemTag)
                    return true;
            }
            return false;
        }
        
    }

    public interface ICoolDown<T> : IComparable<T>
    {
        int ItemTag
        {
            get;
        }
        float NowCoolDown
        {
            get;
            set;
        }
    }
    
}